/**
 * 
 */
package com.thor.palmmall.rest.workorder.converter;

import org.springframework.core.convert.converter.Converter;

import com.thor.commons.entity.UCN;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.common.AAttachmentConverter;
import com.thor.palmmall.rest.common.AOperateInfoConverter;
import com.thor.palmmall.rest.workorder.AWorkOrderBill;
import com.thor.palmmall.rest.workorder.AWorkOrderEvaluation;
import com.thor.palmmall.rest.workorder.AWorkOrderPriority;
import com.thor.palmmall.spi.workorder.SWorkOrderBill;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class AWorkOrderBillConverter implements Converter<SWorkOrderBill, AWorkOrderBill> {

  public static AWorkOrderBillConverter getInstance() {
    if (instance == null)
      instance = new AWorkOrderBillConverter();
    return instance;
  }

  private static AWorkOrderBillConverter instance;

  private AWorkOrderBillConverter() {
  }

  @Override
  public AWorkOrderBill convert(SWorkOrderBill source) {
    if (source == null) {
      return null;
    }
    AWorkOrderBill target = new AWorkOrderBill();
    target.setUuid(source.getUuid());
    target.setVersion(source.getVersion());
    target.setVersionTime(source.getVersionTime());
    target.setCreateInfo(AOperateInfoConverter.getInstance().convert(source.getCreateInfo()));
    target
        .setLastModifyInfo(AOperateInfoConverter.getInstance().convert(source.getLastModifyInfo()));
    target.setPermGroupId(source.getPermGroupId());
    target.setPermGroupTitle(source.getPermGroupTitle());
    target.setBillNumber(source.getBillNumber());
    target.setBizState(source.getBizState());
    target.setBpmInstance(source.getBpmInstance());
    target.setBpmMessage(source.getBpmMessage());
    target.setBpmOutgoingState(source.getBpmOutgoingState());
    target.setBpmState(source.getBpmState());
    target.setAbortMsg(source.getAbortMsg());
    target.setStore(UCN.newInstance(source.getStore()));
    target.setContract(UCN.newInstance(source.getContract()));
    target.setContact(UCN.newInstance(source.getContact()));
    target.setAttachments(
        ConverterUtil.convert(source.getAttachments(), AAttachmentConverter.getInstance()));

    target.setSource(source.getSource());

    target.setCounterpart(UCN.newInstance(source.getCounterpart()));

    target.setSubject(source.getSubject());
    if (source.getPriority() != null) {
      target.setPriority(AWorkOrderPriority.valueOf(source.getPriority().name()));
    }
    target.setContactInfo(source.getContactInfo());
    target.setDescription(source.getDescription());
    target.setValuationUser(UCN.newInstance(source.getValuationUser()));
    target.setValuationTime(source.getValuationTime());
    if (source.getValuationLevel() != null) {
      target.setValuationLevel(AWorkOrderEvaluation.valueOf(source.getValuationLevel().name()));
    }
    target.setValuationContent(source.getValuationContent());
    target.setProcessingLogUser(UCN.newInstance(source.getProcessingLogUser()));
    target.setProcessingLogTime(source.getProcessingLogTime());
    target.setProcessingLogContent(source.getProcessingLogContent());
    target.setBeginProcessUser(source.getBeginProcessUser());
    target.setBeginProcessTime(source.getBeginProcessTime());
    target.setFinishProcessTime(source.getFinishProcessTime());
    target.setOrders(
        ConverterUtil.convert(source.getOrders(), ABusinessOrderConverter.getInstance()));

    return target;
  }
}
